<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="报警类型" prop="wPassageway">
        <el-select
          v-model="queryParams.wPassageway"
          placeholder="报警类型"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in wTypelist"
            :key="dict.id"
            :label="dict.wType"
            :value="dict.wPassageway"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="报警时间" prop="wDate">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.wDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择报警时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="报警时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="处理内容" prop="wDesc">
        <el-input
          v-model="queryParams.wDesc"
          placeholder="请输入处理内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aiWarnHistoryList">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>  
        </template>
      </el-table-column>
      <el-table-column label="IP" align="center" prop="wIp" />
      <el-table-column
        label="报警通道"
        align="center"
        prop="wPassageway"
        show-overflow-tooltip
      />
      <el-table-column
        label="报警名称"
        align="center"
        prop="wName"
        show-overflow-tooltip
      />
      <el-table-column
        label="报警类型"
        align="center"
        prop="wType"
        show-overflow-tooltip
      />
      <el-table-column label="报警时间" align="center" prop="wDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.wDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="处理时间"
        align="center"
        prop="wHandleTime"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="处理内容"
        align="center"
        prop="wDesc"
        show-overflow-tooltip
      />
      <el-table-column
        label="处理结果"
        align="center"
        prop="status"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.status == '0' ? '未处理' : '已处理' }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createtime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['video:aiWarnHistory:edit']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleManage(scope.row)"
            v-hasPermi="['video:aiWarnHistory:edit']"
            >处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="IP" prop="wIp">
          <el-input v-model="form.wIp" placeholder="IP" readonly="" />
        </el-form-item>
        <el-form-item label="报警通道" prop="wPassageway">
          <el-input
            v-model="form.wPassageway"
            placeholder="报警通道"
            readonly=""
          />
        </el-form-item>
        <el-form-item label="报警名称" prop="wName">
          <el-input v-model="form.wName" placeholder="报警名称" readonly="" />
        </el-form-item>
        <el-form-item label="报警文件" prop="wFile">
          <!-- <fileUpload v-model="form.wFile" /> -->
          <el-input v-model="form.wFile" placeholder="报警文件" readonly="" />
        </el-form-item>
        <el-form-item label="报警时间" prop="wDate">
          <el-input v-model="form.wDate" placeholder="报警时间" readonly="" />
        </el-form-item>
        <el-form-item label="处理时间" prop="wHandleTime">
          <el-input
            v-model="form.wHandleTime"
            placeholder="处理时间"
            readonly=""
          />
        </el-form-item>
        <el-form-item label="处理内容" prop="wDesc">
          <el-input v-model="form.wDesc" placeholder="处理内容" readonly="" />
        </el-form-item>
        <el-form-item label="处理结果" prop="status">
          <el-input v-model="onChange" placeholder="处理结果" readonly="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="title"
      :visible.sync="handle"
      width="500px"
      append-to-body
    >
      <el-form
        ref="handleform"
        :model="handleform"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="处理时间" prop="wHandleTime">
          <el-date-picker
            clearable
            size="small"
            v-model="handleform.wHandleTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="选择处理时间"
            style="width: 380px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理内容" prop="wDesc">
          <el-input v-model="handleform.wDesc" placeholder="处理内容" />
        </el-form-item>
        <el-form-item label="处理结果" prop="status">
          <el-select
            v-model="handleform.status"
            placeholder="请选择处理结果"
            style="width: 380px"
          >
            <el-option label="已处理" value="1"></el-option>
            <el-option label="未处理" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="handlecancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAiWarnHistory,
  getAiWarnHistory,
  delAiWarnHistory,
  addAiWarnHistory,
  updateAiWarnHistory
} from '@/api/video/aiWarnHistory'
import { listAiWarnConfig } from '@/api/video/aiWarnConfig'
export default {
  name: 'AiWarnHistory',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警记录表格数据
      aiWarnHistoryList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      handle: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wIp: null,
        wPassageway: null,
        wName: null,
        wType: null,
        wFile: null,
        wDesc: null,
        wDate: null,
        wHandleTime: null,
        createtime: null
      },
      // 表单参数
      form: {},
      handleform: {
        status: '',
        wHandleTime: ''
      },
      // 表单校验
      rules: {
        wHandleTime: [
          {
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        status: [
          { required: true, message: '请选择处理结果', trigger: 'change' }
        ]
      },
      // 报警类型
      wTypelist: [],
      wTypeParams: {
        pageNum: 1,
        pageSize: 100
      },
      // 日期范围
      dateRange: []
    }
  },
  created() {
    this.getList()
    this.getwTypelist()
  },
  computed: {
    onChange: {
      get() {
        if (this.form.status == '0') {
          return '未处理'
        } else {
          return '已处理'
        }
      },
      set(val) {
        if (this.form.status == '0') {
          this.form.status = val
        } else {
          this.form.status = val
        }
      }
    }
  },
  methods: {
    getwTypelist() {
      listAiWarnConfig(this.wTypeParams).then(response => {
        this.wTypelist = response.rows
        console.log(this.wTypelist)
      })
    },
    /** 查询报警记录列表 */
    getList() {
      this.loading = true
      listAiWarnHistory(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then(response => {
        this.aiWarnHistoryList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    handlecancel() {
      this.handle = false
      this.handlereset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wIp: null,
        wPassageway: null,
        wName: null,
        wType: null,
        wFile: null,
        wDesc: null,
        wDate: null,
        statue: null,
        wHandleTime: null,
        createtime: null
      }
      this.resetForm('form')
      this.dateRange = []
    },
    handlereset() {
      this.handleform = {
        id: null,
        status: null,
        wHandleTime: null,
        wDesc: null
      }
      this.resetForm('handleform')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset()
      const id = row.id || this.ids
      getAiWarnHistory(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '报警记录详情'
      })
    },
    /** 处理按钮操作 */
    handleManage(row) {
      this.handlereset()
      const id = row.id || this.ids
      getAiWarnHistory(id).then(response => {
        if (
          response.data.wHandleTime != '' &&
          response.data.wHandleTime != null
        ) {
          this.handleform.wHandleTime = response.data.wHandleTime
        } else {
          this.date()
        }
        this.handleform.status = response.data.status
        this.handleform.wDesc = response.data.wDesc
        this.handleform.id = response.data.id
        this.handle = true
        this.title = '处理报警信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['handleform'].validate(valid => {
        if (valid) {
          updateAiWarnHistory(this.handleform).then(response => {
            this.msgSuccess('处理成功')
            this.handle = false
            this.getList()
          })
        }
      })
    },
    date() {
      const nowDate = new Date()
      const date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        date: nowDate.getDate(),
        hours: nowDate.getHours(),
        minutes: nowDate.getMinutes(),
        seconds: nowDate.getSeconds()
      }
      const newmonth = date.month > 10 ? date.month : '0' + date.month
      const newday = date.date > 10 ? date.date : '0' + date.date
      const newminutes = date.minutes < 10 ? '0' + date.minutes : date.minutes
      const newseconds = date.seconds < 10 ? '0' + date.seconds : date.seconds
      this.handleform.wHandleTime =
        date.year +
        '-' +
        newmonth +
        '-' +
        newday +
        ' ' +
        date.hours +
        ':' +
        newminutes +
        ':' +
        newseconds
    }
  }
}
</script>
